package com.isunland.managesystem.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.MessageContact;
import com.isunland.managesystem.utils.MyStringUtil;
import java.util.List;

@SuppressWarnings("NullableProblems") public class MessageContactAdapter
    extends ArrayAdapter<MessageContact> {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-07-27 17:04:41 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final LinearLayout rootView;
    public final TextView tvName;
    final TextView tvDeptName;
    final TextView tvPhoneNumber;
    final TextView tvEMailContacts;
    final TextView tvEmergencyContacts;
    final TextView ivLogo;
    final ImageView ivCall;

    private ViewHolder(LinearLayout rootView, TextView tvName, TextView tvDeptName,
        TextView tvPhoneNumber, TextView tvEMailContacts, TextView tvEmergencyContacts,
        ImageView ivCall, TextView ivLogo) {
      this.rootView = rootView;
      this.tvName = tvName;
      this.tvDeptName = tvDeptName;
      this.tvPhoneNumber = tvPhoneNumber;
      this.tvEMailContacts = tvEMailContacts;
      this.tvEmergencyContacts = tvEmergencyContacts;
      this.ivCall = ivCall;
      this.ivLogo = ivLogo;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView tvName = (TextView) rootView.findViewById(R.id.tv_name_contacts);
      TextView tvDeptName = (TextView) rootView.findViewById(R.id.tv_deptName_contacts);
      TextView tvPhoneNumber = (TextView) rootView.findViewById(R.id.tv_phoneNumber_contacts);
      ImageView ivCall = (ImageView) rootView.findViewById(R.id.iv_call_contacts);
      TextView tvEMailContacts = (TextView) rootView.findViewById(R.id.tv_eMail_contacts);
      TextView tvEmergencyContacts = (TextView) rootView.findViewById(R.id.tv_emergency_contacts);
      TextView ivLogo = (TextView) rootView.findViewById(R.id.iv_logo);
      return new ViewHolder(rootView, tvName, tvDeptName, tvPhoneNumber, tvEMailContacts,
          tvEmergencyContacts, ivCall, ivLogo);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_contact, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    MessageContact item = getItem(position);
    if (item == null) return vh.rootView;
    //Bind your data to the views here
    vh.tvName.setText(item.getName());
    vh.ivLogo.setText(MyStringUtil.substring(item.getName(), 0, 1));
    vh.tvDeptName.setText(MyStringUtil.isEmpty(item.getDeptName()) ? "暂无信息" : item.getDeptName());
    vh.tvPhoneNumber.setText(MyStringUtil.isEmpty(item.getMobile()) ? "暂无信息" : item.getMobile());
    vh.tvEMailContacts.setText(MyStringUtil.isEmpty(item.getEmail()) ? "暂无信息" : item.getEmail());
    vh.tvEmergencyContacts.setText(
        getContext().getString(R.string.emergency) + (MyStringUtil.isEmpty(item.getEmergencyCall())
            ? "暂无信息" : item.getEmergencyCall()));
    return vh.rootView;
  }

  private LayoutInflater mInflater;

  // Constructors
  public MessageContactAdapter(Context context, List<MessageContact> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  public MessageContactAdapter(Context context, MessageContact[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  @Override public boolean isEmpty() {
    return false;
  }
}
